package com.egu.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.egu.service.CustomerService;
import com.egu.service.ReportService;
import com.egu.vo.ConditionReportVo;
import com.egu.vo.CustomerVo;
import com.egu.vo.DistrReportVo;
import com.egu.vo.RequestResultVo;
import com.egu.vo.SourceReportVo;
import com.egu.vo.TypeReportVo;

@Service
public class ReportServiceImpl implements ReportService {

	@Autowired
	CustomerService customerService;

	@Override
	public Map<String, Object> getReportData() {
		RequestResultVo<List<CustomerVo>> resultVo = customerService.getAllList();
		List<CustomerVo> list = resultVo.getResult();
		/*
		 * Map<String, String> mapType = new HashMap<>(); Map<String, String> mapSource
		 * = new HashMap<>(); Map<String, String> mapCondition = new HashMap<>();
		 * 
		 * for (CustomerVo customerVo : list) { mapType.put(customerVo.getTypeName(),
		 * customerVo.getTypeName()); mapSource.put(customerVo.getSourceName(),
		 * customerVo.getSourceName()); mapCondition.put(customerVo.getConditionName(),
		 * customerVo.getConditionName()); }
		 */
		// 得到 typereport
		List<TypeReportVo> typeList = new ArrayList<>();
		//Map<String, TypeReportVo> map111 = new HashMap<>();
		for (CustomerVo customerVo : list) {
			String typeName = customerVo.getTypeName();
			TypeReportVo typeReportVo = new TypeReportVo();
			typeReportVo.setTypeName(typeName);
			typeReportVo.setNum(1);

			//
			for (int i = 0; i < typeList.size(); i++) {
				if (typeList.get(i).getTypeName().equals(typeName)) {
					typeList.get(i).setNum(typeList.get(i).getNum() + 1);
					break;
				} else {
					int j = i + 1;
					if (j >= typeList.size()) {
						typeList.add(typeReportVo);
						break;
					}

				}
			}
			if (typeList.size() == 0) {
				typeList.add(typeReportVo);
			}
/*
			if(map111.get(customerVo.getTypeName())==null){
				TypeReportVo reportVo = new TypeReportVo();
				reportVo.setTypeName(customerVo.getTypeName());
				reportVo.setNum(0);
				map111.put(customerVo.getTypeName(), reportVo);
				break;
			}
			if (customerVo.getTypeName().equals(map111.get(customerVo.getTypeName()).getTypeName())) {
				TypeReportVo reportVo = map111.get(customerVo.getTypeName());
				reportVo.setNum(reportVo.getNum() + 1);
			} */
		}

		
		// 得到sourcereport
		List<SourceReportVo> sourceList = new ArrayList<>();
		for (CustomerVo customerVo : list) {
			String sourceName = customerVo.getSourceName();
			SourceReportVo sourceReportVo = new SourceReportVo();
			sourceReportVo.setSourceName(sourceName);
			sourceReportVo.setNum(1);
			for (int i = 0; i < sourceList.size(); i++) {
				if (sourceList.get(i).getSourceName().equals(sourceName)) {
					sourceList.get(i).setNum(sourceList.get(i).getNum() + 1);
					break;
				} else {
					int j = i + 1;
					if (j >= sourceList.size()) {
						sourceList.add(sourceReportVo);
						break;
					}
				}
			}

			if (sourceList.size() == 0) {
				sourceList.add(sourceReportVo);
			}
		}
		
		// 得到conditionreport
		List<ConditionReportVo> conditionList = new ArrayList<>();
		for (CustomerVo customerVo : list) {
			String conditionName = customerVo.getConditionName();
			ConditionReportVo conditionReportVo = new ConditionReportVo();
			conditionReportVo.setConditionName(conditionName);
			conditionReportVo.setNum(1);

			for (int i = 0; i < conditionList.size(); i++) {
				if (conditionList.get(i).getConditionName().equals(conditionName)) {
					conditionList.get(i).setNum(conditionList.get(i).getNum() + 1);
					break;
				} else {
					int j = i + 1;
					if (j >= conditionList.size()) {
						conditionList.add(conditionReportVo);
						break;
					}
				}
			}

			if (conditionList.size() == 0) {
				conditionList.add(conditionReportVo);
			}
		}

		// 得到用户分配
		List<DistrReportVo> distrList = new ArrayList<>();
		int distrNum = 0;
		int noDistrNum = 0;
		for (CustomerVo customerVo : list) {
			if (customerVo.getUserId() == null) {
				++noDistrNum;
			} else {
				++distrNum;
			}
		}
		DistrReportVo distrReportVo = new DistrReportVo();
		distrReportVo.setDistrName("已经分配");
		distrReportVo.setNum(distrNum);
		DistrReportVo noDistrReportVo = new DistrReportVo();
		noDistrReportVo.setDistrName("未分配");
		noDistrReportVo.setNum(noDistrNum);
		distrList.add(distrReportVo);
		distrList.add(noDistrReportVo);

		Map<String, Object> map = new HashMap<>();
		map.put("typeReportList", typeList);
		map.put("sourceReportList", sourceList);
		map.put("conditionRportList", conditionList);

		map.put("distrReportList", distrList);

		return map;
	}
}
